//student页面伪造的数据
var List = []
import Mock from 'mockjs'
// 导入字符串分割工具
import { param2Obj } from '@/utils/urlUtils'
// 循环生成模拟数据
for (var i = 0; i < 100; i++) {
    List.push({
        // 真实id
        id: Mock.mock('@guid()'),
        userid: '00' + Mock.mock('@string("number", 5)'),
        usernick: Mock.mock('@cfirst()') + Mock.mock('@cname()'),
        memberphone: '1' + Mock.mock('@string("number", 10)'),
        pay: Mock.mock('@natural(1000, 5000)'),
        orders: Mock.mock('@natural(5, 50)'),
        memberlv: Mock.mock('@natural(1, 3)'),
        date: Mock.mock('@datetime("yyyy-MM-dd HH:mm")'),
        disableuser: Mock.mock('@boolean(1, 2, true)'),
    })
}

// 后端处理请求的函数，再返回数据给前端
export default {
    getStudentData: config => {
        // console.log('config',param2Obj(config.url))
        var { keyword, startTime, endTime, page, pageSize } = param2Obj(config.url)
        // var res = List.filter(item=>item.usernick.indexOf(keyword) != -1 || item.memberphone.indexOf(keyword) !=-1) || (Date.parse(item.date) >= Date.parse(startTime) && Date.parse(item.date) <= Date.parse(endTime))

        var res = List.filter((item) => {
            if (startTime == "" || endTime == "") {
                return List
            } else {
                return Date.parse(item.date) >= Date.parse(startTime) && Date.parse(item.date) <= Date.parse(endTime)
            }
        })

        var res2 = res.filter(item => item.usernick.indexOf(keyword) != -1 || item.memberphone.indexOf(keyword) != -1)
        // console.log('res2',res2);

        var resLimit = res2.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })
        return {
            code: 200,
            msg: '请求成功',
            data: resLimit,
            total: res2.length
        }
    },
    DelData: config => {
        console.log('config', config);
        // 对象的结构拿出id
        var { id } = JSON.parse(config.body)
        // 过滤出原数组中不等于传来的id的数据
        var res = List.filter(item => item.id != id)
        // 分页处理后的数据，只拿固定索引下标的数据此处用slice进行分割
        // var respage = res.slice((page-1)*pageSize,page*pageSize);
        // var resLimit = res.filter((item,index)=>{
        //     return (page-1) * pageSize <= index && index <= page * pageSize
        // })
        // console.log('res',res);
        List = res
        return {
            code: 200,
            msg: '删除成功',
            data: null,
        }
    },
    batchDelData: config => {
        // 获取所有的id,然后批量删除
        var { ids } = JSON.parse(config.body)
        var res = List.filter(item => !ids.includes(item.id))
        List = res
        return ({
            code: 200,
            msg: '批量删除成功',
            data: null,
        })
    },

    addStdData: config => {
        // 获取前端传来的数据
        const { usernick, memberphone, pay, orders, memberlv, date, disableuser } = JSON.parse(config.body)
        List.unshift({
            id: Mock.mock('@guid()'),
            userid: '00' + Mock.mock('@string("number", 5)'),
            usernick: usernick,
            memberphone: memberphone,
            pay: pay,
            orders: orders,
            memberlv: memberlv,
            date: date,
            disableuser: disableuser,
        })
        return ({
            code: 200,
            msg: '添加成功',
            data: null,
        })
    },

    editStdData: config => {
        const { id, userid, usernick, memberphone, pay, orders, memberlv, date, disableuser } = JSON.parse(config.body)
        List.forEach(item => {
            if (item.id == id) {
                item.userid = userid
                item.usernick = usernick
                item.memberphone = memberphone
                item.pay = pay
                item.orders = orders
                item.memberlv = memberlv
                item.date = date
                item.disableuser = disableuser
                return
            }
        })
        return ({
            code: 200,
            msg: '修改成功',
            data: null,
        })
    }


}